package com.weipai.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.search.DKeyWordAttributeMapper;
import com.weipai.demo.mapper.search.DKeyWordMapper;
import com.weipai.demo.mapper.search.KeyWordAttributeMapper;
import com.weipai.demo.mapper.search.KeyWordMapper;
import com.weipai.demo.model.KeyWord;
import com.weipai.demo.model.KeyWordAttribute;
import com.weipai.demo.service.KeyWordService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;




/**
 * @Author: 赵项阳
 * @Date: 2019/7/2 16:52
 * @Version 1.0
 */
@Service
public class KeyWordServiceImpl implements KeyWordService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private KeyWordMapper keyWordMapper;
    @Autowired
    private KeyWordAttributeMapper keyWordAttributeMapper;
    @Autowired
    private DKeyWordMapper dKeyWordMapper;
    @Autowired
    private DKeyWordAttributeMapper dKeyWordAttributeMapper;


    //填写关键字
    @Override
    public Response save(KeyWord keyWord) {
        Response response = new Response();
        try {
            int rest = 0;
            if(keyWord.getId() == null){
                //添加时不能有重复的关键字
                int i = keyWordMapper.selectkeyword(keyWord.getKeyword());
                if (i > 0) {
                    response.setCode(RetCode.IDENTICAL);
                    return response;
                }
                Date createTime = new Date();
                keyWord.setCreateTime(createTime);
                keyWord.setStatus(1);
                rest = dKeyWordMapper.insert(keyWord);
            }else{
                rest = dKeyWordMapper.updateByPrimaryKey(keyWord);
            }
           if(rest > 0){
               response.setCode(RetCode.SUCCESS);
           }else{
               response.setCode(RetCode.UPDATEERROR);
           }
//            int kId = keyWordMapper.selectBykeyword(keyWord);
//
//
//            String str = attribute;
//            String[] split = str.split(",");
//            for (int j = 0; j < split.length; j++) {
//
//                KeyWordAttribute KeyWordAttribute = new KeyWordAttribute();
//                KeyWordAttribute.setAttribute(split[j]);
//                KeyWordAttribute.setCreateTime(createTime);
//                KeyWordAttribute.setkId(kId);
//                KeyWordAttribute.setStatus(status);
//                KeyWordAttribute.setType(type);
//                int insert1 = dKeyWordAttributeMapper.insert(KeyWordAttribute);
//            }
//            response.setCode(RetCode.SUCCESS);
        } catch (Exception e) {
 
            logger.error("save" + e.getMessage());
            response.setCode(RetCode.REQUEST_ERROR);
            response.setData(e.getMessage());
        }
        return response;
    }


    //前台展示关键字
    @Override
    public Response frontShow(Integer type) {
        Response response = new Response();
        List<Map<String, Object>> list = keyWordMapper.getCategory(type);

        if (list != null) {
            Map<String, Map<String, Object>> returnList = new HashMap<>();
            for (Map<String, Object> item : list) {

                List<String> attrList = keyWordAttributeMapper.selectAll((Integer) item.get("id"));
                if (attrList != null) {
                    item.put("item", attrList);
                }
                returnList.put((String) item.get("title"), item);

            }
            response.setData(returnList);
            response.setCode(RetCode.SUCCESS);
            return response;
        }

        response.setCode(RetCode.REQUEST_ERROR);
        return response;
    }


    //前台关键字查询
    @Override
    public Response searchKeyword() {
        return null;
    }

    @Override
    public Response saveAttribute(KeyWordAttribute keyWordAttribute){
        Response response = new Response();
        Integer rest;
        keyWordAttribute.setCreateTime(new Date());
        if(keyWordAttribute.getId()!= null){
            rest = dKeyWordAttributeMapper.updateByPrimaryKey(keyWordAttribute);
        }else{
            rest = dKeyWordAttributeMapper.insert(keyWordAttribute);
        }
        if(rest > 0){
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return  response;
    }

    //后台分页展示关键字
    @Override
    public LayuiTablePagingResult show(Integer page, Integer size, Integer type) {
        LayuiTablePagingResult result = null;
        try {
            PageHelper.startPage(page, size);
            List<Map<String, Object>> list = keyWordMapper.selectByType(type);
            if (list != null) {
                for (Map<String, Object> item : list) {
                    Map<String,Object> list2 = keyWordAttributeMapper.selectByStatus((Integer)item.get("id"));
                    if(list2 != null){
                        item.put("attribute",list2.get("attribute"));
                    }

                }
            }
//            List<String> list = keyWordAttributeMapper.selectByStatus(type);

            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(RetCode.REQUEST_ERROR.getCode());
            result.setMsg(RetCode.REQUEST_ERROR.getMsg());
            result.setData(e.getMessage());
            logger.error("show" + e.getMessage());
        }
        return result;
    }

    //后台分页展示关键字
    @Override
    public LayuiTablePagingResult attributeShow(Integer page, Integer size, Integer id) {
        LayuiTablePagingResult result = null;
        try {
            PageHelper.startPage(page, size);
            List<KeyWordAttribute> list = keyWordAttributeMapper.selectAllByStatus(id);

//            List<String> list = keyWordAttributeMapper.selectByStatus(type);

            PageInfo<KeyWordAttribute> pageInfo = new PageInfo<>(list);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(RetCode.REQUEST_ERROR.getCode());
            result.setMsg(RetCode.REQUEST_ERROR.getMsg());
            result.setData(e.getMessage());
            logger.error("show" + e.getMessage());
        }
        return result;

    }

    //修改关键字
    @Override
    public Response updateStatus(Integer id, Integer status, String attribute, Integer type, String keyword, String title) {
        Response response = new Response();
        if (id != null) {
            if (keyword != null || !"".equals(keyword)) {
                int i = keyWordMapper.updateBykeyword(keyword, title, id);
                if (i == 0) {
                    response.setCode(RetCode.REQUEST_ERROR);
                    return response;
                }

//                int rest = keyWordAttributeMapper.delByKid(id);
//                if (rest == 0) {
//                    response.setCode(RetCode.REQUEST_ERROR);
//                    return response;
//                }
//
//                String str = attribute;
//                String[] split = str.split(",");
//                for (int j = 0; j < split.length; j++) {
//                    Date createTime = new Date();
//                    KeyWordAttribute keyWordAttribute = new KeyWordAttribute();
//                    keyWordAttribute.setAttribute(split[j]);
//                    keyWordAttribute.setCreateTime(createTime);
//                    keyWordAttribute.setkId(id);
//                    keyWordAttribute.setStatus(status);
//                    keyWordAttribute.setType(type);
//                    int insert1 = dKeyWordAttributeMapper.insert(keyWordAttribute);
//                }
//            } else {
//                keyWordMapper.delById(id);
//                int rest = keyWordAttributeMapper.delByKid(id);
//                if (rest == 0) {
//                    response.setCode(RetCode.REQUEST_ERROR);
//                    return response;
////                }
                response.setCode(RetCode.SUCCESS);
            }else{
                response.setCode(RetCode.REQUEST_ERROR);
//            int i = keyWordAttributeMapper.updateByPrimaryKey(KeyWordAttribute)
            }
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //删除关键字
    @Override
    public Response delete(Integer id) {
        Response response = new Response();
        if (id != null) {
            int rest = keyWordMapper.delById(id);
            int rest1 = keyWordAttributeMapper.delByKid(id);
//            int i = keyWordAttributeMapper.deleteById(id);
//            int rest = keyWordAttributeMapper.delByKid(id);
            if (rest == 0) {
                response.setCode(RetCode.REQUEST_ERROR);
                return response;
            }
            response.setCode(RetCode.SUCCESS);
            return response;
        } else {
            response.setCode(RetCode.RESOURCES_ADD_FAIL);
            return response;
        }

    }

    @Override
    public Response delAttribute(Integer id){
        Response response = new Response();
        int rest = keyWordAttributeMapper.delById(id);
        if(rest > 0){
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

}
